home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / fish / 676-700 / 681 / term / source.lha / termHotkeys.c < prev    next >
C/C++ Source or Header  |  1992-05-09  |  5KB  |  287 lines

  1. /*
  2. **    $Id: termHotkeys.c,v 1.1 92/04/03 20:43:18 olsen Sta Locker: olsen $
  3. **    $Revision: 1.1 $
  4. **    $Date: 92/04/03 20:43:18 $
  5. **
  6. **    Hotkey support routines
  7. **
  8. **    Copyright © 1990-1992 by Olaf `Olsen' Barthel & MXM
  9. **        All Rights Reserved
  10. */
  11.  
  12. #include "termGlobal.h"
  13.  
  14. enum    {    CX_TERMSCREENTOFRONT,CX_BUFFERSCREENTOFRONT,CX_SKIPDIALENTRY };
  15.  
  16.     /* Asynchronous hotkey task. */
  17.  
  18. STATIC struct Task *CxTask;
  19.  
  20.     /* Hotkey(STRPTR Code,struct MsgPort *Port,LONG ID):
  21.      *
  22.      *    A custom version of the amiga.lib supplied code.
  23.      */
  24.  
  25. STATIC CxObj * __regargs
  26. CustomHotKey(STRPTR Code,struct MsgPort *Port,LONG ID)
  27. {
  28.     CxObj *Filter;
  29.  
  30.     if(Filter = CxFilter(Code))
  31.     {
  32.         CxObj *Sender;
  33.  
  34.         if(Sender = CxSender(Port,ID))
  35.         {
  36.             CxObj *Translator;
  37.  
  38.             AttachCxObj(Filter,Sender);
  39.  
  40.             if(Translator = CxTranslate(NULL))
  41.             {
  42.                 AttachCxObj(Filter,Translator);
  43.  
  44.                 if(!CxObjError(Filter))
  45.                     return(Filter);
  46.             }
  47.         }
  48.  
  49.         DeleteCxObjAll(Filter);
  50.     }
  51.  
  52.     return(NULL);
  53. }
  54.  
  55.     /* CreateBroker(struct MsgPort *CxPort):
  56.      *
  57.      *    Set up a CxObj commodity broker.
  58.      */
  59.  
  60. STATIC CxObj *
  61. CreateBroker(struct MsgPort *CxPort)
  62. {
  63.     CxObj *Broker;
  64.  
  65.         /* Set the commodity priority. */
  66.  
  67.     NewTermBroker . nb_Pri = Hotkeys . CommodityPriority;
  68.  
  69.         /* Create the broker. */
  70.  
  71.     if(Broker = CxBroker(&NewTermBroker,NULL))
  72.     {
  73.             /* Add the hotkeys. */
  74.  
  75.         AttachCxObj(Broker,CustomHotKey(Hotkeys . termScreenToFront,    CxPort,CX_TERMSCREENTOFRONT));
  76.         AttachCxObj(Broker,CustomHotKey(Hotkeys . BufferScreenToFront,    CxPort,CX_BUFFERSCREENTOFRONT));
  77.         AttachCxObj(Broker,CustomHotKey(Hotkeys . SkipDialEntry,    CxPort,CX_SKIPDIALENTRY));
  78.  
  79.             /* Did an error show up? */
  80.  
  81.         if(!CxObjError(Broker))
  82.         {
  83.                 /* Broker has been added, now activate it. */
  84.  
  85.             ActivateCxObj(Broker,Hotkeys . HotkeysEnabled);
  86.  
  87.             return(Broker);
  88.         }
  89.  
  90.         DeleteCxObjAll(Broker);
  91.     }
  92.  
  93.     return(NULL);
  94. }
  95.  
  96.     /* TermCxServer():
  97.      *
  98.      *    Asynchronous hotkey server.
  99.      */
  100.  
  101. STATIC VOID __saveds
  102. TermCxServer()
  103. {
  104.     CxObj        *Broker;
  105.     struct MsgPort    *CxPort;
  106.     CxMsg        *Message;
  107.  
  108.         /* Create a reply port. */
  109.  
  110.     if(CxPort = CreateMsgPort())
  111.     {
  112.             /* Add the port to the public list. */
  113.  
  114.         CxPort -> mp_Node . ln_Name = NewTermBroker . nb_Name;
  115.  
  116.         AddPort(CxPort);
  117.  
  118.             /* Install the port. */
  119.  
  120.         NewTermBroker . nb_Port    = CxPort;
  121.  
  122.             /* Create the broker. */
  123.  
  124.         if(Broker = CreateBroker(CxPort))
  125.         {
  126.             ULONG    SignalSet;
  127.             BYTE    Terminated = FALSE;
  128.  
  129.                 /* Signal father task that we're done. */
  130.  
  131.             Signal(ThisProcess,SIGBREAKF_CTRL_C);
  132.  
  133.                 /* Loop and loop... */
  134.  
  135.             while(!Terminated)
  136.             {
  137.                     /* Wait for some signal. */
  138.  
  139.                 SignalSet = Wait(SIGBREAKF_CTRL_C | SIGBREAKF_CTRL_D | (1 << CxPort -> mp_SigBit));
  140.  
  141.                     /* ^C aborts. */
  142.  
  143.                 if(SignalSet & SIGBREAKF_CTRL_C)
  144.                     Terminated = TRUE;
  145.  
  146.                     /* ^D removes the broker and
  147.                      * creates a new one.
  148.                      */
  149.  
  150.                 if(SignalSet & SIGBREAKF_CTRL_D)
  151.                 {
  152.                     DeleteCxObjAll(Broker);
  153.  
  154.                     Broker = CreateBroker(CxPort);
  155.                 }
  156.  
  157.                     /* A commodity message. */
  158.  
  159.                 if(SignalSet & (1 << CxPort -> mp_SigBit))
  160.                 {
  161.                     ULONG MessageType,MessageID;
  162.  
  163.                         /* Remove all messages. */
  164.  
  165.                     while(Message = (CxMsg *)GetMsg(CxPort))
  166.                     {
  167.                             /* Extract type and ID. */
  168.  
  169.                         MessageType    = CxMsgID(Message);
  170.                         MessageID    = CxMsgType(Message);
  171.  
  172.                         ReplyMsg((struct Message *)Message);
  173.  
  174.                             /* Take a look at the type... */
  175.  
  176.                         switch(MessageID)
  177.                         {
  178.                                 /* A hotkey was pressed. */
  179.  
  180.                             case CXM_IEVENT:    switch(MessageType)
  181.                                         {
  182.                                             case CX_TERMSCREENTOFRONT:    BumpWindow(TopWindow);
  183.                                                             break;
  184.  
  185.                                             case CX_BUFFERSCREENTOFRONT:    if(BufferProcess)
  186.                                                                 Signal(BufferProcess,SIGBREAKF_CTRL_D);
  187.  
  188.                                                             break;
  189.  
  190.                                             case CX_SKIPDIALENTRY:        Signal(ThisProcess,SIGBREAKF_CTRL_F);
  191.                                                             break;
  192.                                         }
  193.  
  194.                                         break;
  195.  
  196.                                 /* An internal commodity command. */
  197.  
  198.                             case CXM_COMMAND:    switch(MessageType)
  199.                                         {
  200.                                             case CXCMD_DISABLE:    ActivateCxObj(Broker,Hotkeys . HotkeysEnabled = FALSE);
  201.                                                         break;
  202.  
  203.                                             case CXCMD_ENABLE:    ActivateCxObj(Broker,Hotkeys . HotkeysEnabled = TRUE);
  204.                                                         break;
  205.  
  206.                                             default:        break;
  207.                                         }
  208.  
  209.                                         break;
  210.                         }
  211.                     }
  212.                 }
  213.             }
  214.  
  215.                 /* Remove the broker. */
  216.  
  217.             DeleteCxObjAll(Broker);
  218.         }
  219.  
  220.             /* Remove the port from the public list. */
  221.  
  222.         RemPort(CxPort);
  223.  
  224.             /* Remove all pendig messages. */
  225.  
  226.         while(Message = (CxMsg *)GetMsg(CxPort))
  227.             ReplyMsg((struct Message *)Message);
  228.  
  229.             /* Delete the reply port. */
  230.  
  231.         DeleteMsgPort(CxPort);
  232.     }
  233.  
  234.     Forbid();
  235.  
  236.         /* Clear the task ID. */
  237.  
  238.     CxTask = NULL;
  239.  
  240.         /* Signal father process that we're done. */
  241.  
  242.     Signal(ThisProcess,SIGBREAKF_CTRL_C);
  243. }
  244.  
  245.     /* ShutdownCx():
  246.      *
  247.      *    Remove the hotkey task.
  248.      */
  249.  
  250. VOID
  251. ShutdownCx()
  252. {
  253.     if(CxTask)
  254.     {
  255.         Signal(CxTask,SIGBREAKF_CTRL_C);
  256.  
  257.         Wait(SIGBREAKF_CTRL_C);
  258.     }
  259. }
  260.  
  261.     /* SetupCx():
  262.      *
  263.      *    Create the hotkey task.
  264.      */
  265.  
  266. BYTE
  267. SetupCx()
  268. {
  269.         /* If the task is already running, tell it to
  270.          * update the hotkey settings.
  271.          */
  272.  
  273.     if(CxTask)
  274.         Signal(CxTask,SIGBREAKF_CTRL_D);
  275.     else
  276.     {
  277.         CxTask = CreateTask("term hotkey task",0,TermCxServer,4000);
  278.  
  279.         Wait(SIGBREAKF_CTRL_C);
  280.  
  281.         if(!CxTask)
  282.             return(FALSE);
  283.     }
  284.  
  285.     return(TRUE);
  286. }
  287.